package city.spring.modules.material.controller;

import city.spring.modules.material.entity.MaterialBreakageApprovalEntity;
import city.spring.modules.material.service.MaterialBreakageApprovalService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：物资报损审核表
 *
 * @author 谢金婷
 * @date 2021-01-20 14:22:53
 */
@RestController
@RequestMapping("/material_breakage_approval")
public class MaterialBreakageApprovalController extends ApiController {
    private final MaterialBreakageApprovalService materialBreakageApprovalService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<MaterialBreakageApprovalEntity, ?>> entityOrderFields;

    public MaterialBreakageApprovalController(MaterialBreakageApprovalService materialBreakageApprovalService) {
        this.materialBreakageApprovalService = materialBreakageApprovalService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(MaterialBreakageApprovalEntity::getId);
        entityOrderFields.add(MaterialBreakageApprovalEntity::getMaterialBreakageId);
        entityOrderFields.add(MaterialBreakageApprovalEntity::getReviewer);
        entityOrderFields.add(MaterialBreakageApprovalEntity::getApprovalStatus);
        entityOrderFields.add(MaterialBreakageApprovalEntity::getSuggestion);
        entityOrderFields.add(MaterialBreakageApprovalEntity::getReviewdate);
        entityOrderFields.add(MaterialBreakageApprovalEntity::getType);
        entityOrderFields.add(MaterialBreakageApprovalEntity::getGmtCreate);
        entityOrderFields.add(MaterialBreakageApprovalEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>物资报损审核表</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, MaterialBreakageApprovalEntity entity) {
        LambdaQueryChainWrapper<MaterialBreakageApprovalEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>物资报损审核表</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, MaterialBreakageApprovalEntity entity) {
        LambdaQueryChainWrapper<MaterialBreakageApprovalEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<MaterialBreakageApprovalEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>物资报损审核表</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(materialBreakageApprovalService.getById(id));
    }

    /**
     * 添加一个 <strong>物资报损审核表</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping("/warehouse")
    public Object save(@RequestBody MaterialBreakageApprovalEntity entity) {
        materialBreakageApprovalService.saveMaterialBreakageApproval(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }
    @PostMapping("/delivery")
    public Object add(@RequestBody MaterialBreakageApprovalEntity entity) {
        materialBreakageApprovalService.addMaterialBreakageApproval(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>物资报损审核表</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody MaterialBreakageApprovalEntity entity) {
        entity.setId(id);
        materialBreakageApprovalService.updateMaterialBreakageApproval(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>物资报损审核表</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        materialBreakageApprovalService.deleteMaterialBreakageApproval(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>物资报损审核表</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        materialBreakageApprovalService.deleteMaterialBreakageApproval(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<MaterialBreakageApprovalEntity> buildLambdaQuery(MaterialBreakageApprovalEntity entity) {
        LambdaQueryChainWrapper<MaterialBreakageApprovalEntity> lambdaQuery = materialBreakageApprovalService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), MaterialBreakageApprovalEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getMaterialBreakageId()), MaterialBreakageApprovalEntity::getMaterialBreakageId, entity.getMaterialBreakageId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReviewer()), MaterialBreakageApprovalEntity::getReviewer, entity.getReviewer());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getApprovalStatus()), MaterialBreakageApprovalEntity::getApprovalStatus, entity.getApprovalStatus());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSuggestion()), MaterialBreakageApprovalEntity::getSuggestion, entity.getSuggestion());
        lambdaQuery.eq(entity.getReviewdate() != null, MaterialBreakageApprovalEntity::getReviewdate, entity.getReviewdate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), MaterialBreakageApprovalEntity::getType, entity.getType());
        lambdaQuery.eq(entity.getGmtCreate() != null, MaterialBreakageApprovalEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, MaterialBreakageApprovalEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<MaterialBreakageApprovalEntity> buildQuery(MaterialBreakageApprovalEntity entity) {
        QueryWrapper<MaterialBreakageApprovalEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getMaterialBreakageId()), "material_breakage_id", entity.getMaterialBreakageId());
        wrapper.like(StringUtils.isNotBlank(entity.getReviewer()), "reviewer", entity.getReviewer());
        wrapper.like(StringUtils.isNotBlank(entity.getApprovalStatus()), "approval_status", entity.getApprovalStatus());
        wrapper.like(StringUtils.isNotBlank(entity.getSuggestion()), "suggestion", entity.getSuggestion());
        wrapper.eq(entity.getReviewdate() != null, "reviewdate", entity.getReviewdate());
        wrapper.like(StringUtils.isNotBlank(entity.getType()), "type", entity.getType());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
